﻿/**
 * ...
 * @author 
 */

package racingGame.ui 
{
	import flash.display.Bitmap;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Vector3D;
	
	import away3d.containers.View3D;
	import away3d.materials.BitmapMaterial;
	import away3d.primitives.Sphere;
	
	import racingGame.engine.*;
	import racingGame.events.ScreenEvent;
	import racingGame.assets.Textures;
	import racingGame.assets.Meshes;
	
	

	public class InGame extends MovieClip implements IStackableScreen
	{
		/**** Private Variables ****/
		
		public var _view 		:View3D;
		private var _enabled 	:Boolean;
		private var _exit		:ExitButtonAsset;
		private var _bkgBmp		:Bitmap;
		
		private var _trackEngine 	:TrackTestEngine;
		private var _carEngine		:CarTestEngine;
		
		
		/**** Public Methods ****/
		
		/**
		 * Constructor.
		 */
		public function InGame() 
		{
			super();

			_view = new View3D();
            _view.x = 320;
            _view.y = 240;
			_bkgBmp = new Bitmap(Textures.BACKGROUND);
		}
		
		
		/**
		 * Retrieves the enables status.
		 */
		public function get isEnabled() :Boolean
		{
			return _enabled;
		}
		
		
		/**
		 * Enable this screen.
		 */
		public function enable() :void
		{
			_enabled = true;
			addChild(_view);
			addEventListener(Event.ENTER_FRAME, onEnterFrame);
			
			if (RacingGameMain.testMode == RacingGameMain.CAR_TEST) {
				
				_carEngine = new CarTestEngine(_view);
			}
			else {
				
				_trackEngine = new TrackTestEngine(_view);
			}
			
			initializeHUD();
		}
		
		
		/**
		 * Disable this screen.
		 */
		public function disable() :void
		{
			_enabled = false;
			removeChild(_view);
			removeChild(_exit);
			
			removeEventListener(Event.ENTER_FRAME, onEnterFrame);
			_exit.removeEventListener(MouseEvent.CLICK, onExitClick);
		}
		
		
		/**
		 * Instance and show all the HUD elements.
		 */
		private function initializeHUD() :void
		{
			_exit = new ExitButtonAsset();
			_exit.addEventListener(MouseEvent.CLICK, onExitClick);
			_exit.buttonMode = true;
			_exit.mouseChildren = false;
			_exit.x = 470;
			_exit.y = 10;
			addChild(_exit);
		}
		
		
		/**** Callback Methods ****/
		
		/**
		 * Handle the screen's update. Called each frame.
		 * 
		 * @param	e	The OnEnterFrame event.
		 */
		private function onEnterFrame(event :Event):void
        {
			if (_carEngine) {
				
				_carEngine.update();
			}
			else if (_trackEngine)
			{
				_trackEngine.update();
			}
			
			_view.render();
        }
		

		/**
		 * Handle the click on the Play Button. Goes to the Main menu.
		 * 
		 * @param	e	The click event.
		 */
		private function onExitClick(e :MouseEvent) :void 
		{
			dispatchEvent(new ScreenEvent(ScreenEvent.SCREEN_EXIT, MainMenu));
		}
		
	}

}